package com.trend.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

// 普通pojo实例化到spring容器中
@Component
public class SpringContextUtil implements ApplicationContextAware {

	private SpringContextUtil() {
		System.out.println("SpringContextUtil()");
	}

	// ApplicationContext的BeanFactory 的子类，
	// 拥有更强大的功能，ApplicationContext可以在服务器启动的时候自动实例化所有的bean
	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	public static <T> T getBean(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}

}
